package com.qymerge.manage.data.service.impl;

import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.utils.ReadExcelUtil;
import com.qymerge.manage.data.dao.IPdzMgrDao;
import com.qymerge.manage.data.dto.PdzMgrDto;
import com.qymerge.manage.data.dto.export.PdzMgrExportDTO;
import com.qymerge.manage.data.ro.PdzMgrRO;
import com.qymerge.manage.data.service.IPdzMgrService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *<p>Title: PdzMgrServiceImpl</p>
 *<p>Description: 定制库存</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 18:09
 */
@Service
public class PdzMgrServiceImpl implements IPdzMgrService {

    @Autowired
    private IPdzMgrDao pdzMgrDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(PdzMgrDto ro, PageRequest pageRequest) {
        List list = pdzMgrDao.list(ro, pageRequest);
        DataTableRO dataTableRO = new DataTableRO();
        int allCount = pdzMgrDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public PdzMgrRO detail(Long id) {
        return pdzMgrDao.findOne(id);
    }

    /**
     * 后台管理系统-用户管理-新增
     *
     * @param ro
     * @return
     */
    @Override
    public ActionResultRO save(PdzMgrRO ro) {
        int rn = 0;
        if (ro.getId() == 0) {
            rn = pdzMgrDao.install(ro);
        } else {
            rn = pdzMgrDao.update(ro);
        }
        return new ActionResultRO(rn, ro.getId() == 0 ? "保存成功" : "修改成功", ro.getId() == 0 ? "保存失败" : "修改失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public ActionResultRO delete(Long[] ids) {
        int rn = pdzMgrDao.remove(Arrays.asList(ids));
        return new ActionResultRO(true, "删除" + rn +"条成功", "删除" + rn +"条失败");
    }

    @Override
    public List<PdzMgrExportDTO> exportData(PdzMgrDto ro) {
        List<PdzMgrExportDTO> list = new ArrayList<PdzMgrExportDTO>();
        PdzMgrExportDTO dto = null;

        List<PdzMgrRO> pdzMgrROS = pdzMgrDao.findAll(ro);
        if (CollectionUtils.isNotEmpty(pdzMgrROS)) {
            for (PdzMgrRO pdzMgrRO : pdzMgrROS) {
                dto = new PdzMgrExportDTO();

                dto.setId(pdzMgrRO.getId());
                dto.setYdate(pdzMgrRO.getYdate());
                dto.setYtime(pdzMgrRO.getYtime());
                dto.setNum(pdzMgrRO.getNum());
                dto.setHavenum(pdzMgrRO.getHavenum());

                list.add(dto);
            }
        }

        return list;
    }

    @Override
    public ActionResultRO importData(MultipartFile multipartFile, int flag) {
        StringBuffer tip = new StringBuffer("导入结果：<br/>");
        ReadExcelUtil readExcelUtil = new ReadExcelUtil();
        List<String[][]> datas = null;
        try {
            datas = readExcelUtil.readByStream(multipartFile.getInputStream(), 2, multipartFile.getOriginalFilename());
            if (org.springframework.util.CollectionUtils.isEmpty(datas)) {
                return new ActionResultRO(false, "execl数据为空");
            }
            String[][] data = datas.get(0);

            long id= 0L;
            String ydate= null;
            String ytime= null;
            long num= 0;
            long havenum= 0;

            PdzMgrRO pdzMgrRO = null;
            int rowNum = 1;        // 记录当前行
            int successNum = 0;    // 记录新增或者更新记录数
            for (String[] row : data) {
                rowNum++;

                id = Long.valueOf(row[0]);
                ydate= row[1];
                ytime= row[2];

                if (StringUtils.isNotBlank(row[3])) {
                    num= Long.valueOf(row[3]);
                }
                if (StringUtils.isNotBlank(row[4])) {
                    havenum= Long.valueOf(row[4]);
                }

                if (StringUtils.isEmpty(id + "")
                        || StringUtils.isEmpty(ydate)) {
                    tip.append("第").append(rowNum).append("行：部分数据为空，不予新增<br/>");
                    continue;
                }

                pdzMgrRO = new PdzMgrRO(id, ydate, ytime, num, havenum);

                if (flag == 1) {
                    pdzMgrDao.install(pdzMgrRO);
                    successNum++;
                } else if (flag == 0) {
                    pdzMgrDao.update(pdzMgrRO);
                    successNum++;
                }

            }
            tip.append("成功"+ (flag == 1 ? "新增":"更新") +"记录：").append(successNum).append("条");
        } catch (IOException e) {
            e.printStackTrace();
            return new ActionResultRO(false, "导入失败");
        }

        return new ActionResultRO(true, tip.toString());
    }

}
